package cn.threewaters.qtyy.web.controller.business;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.threewaters.qtyy.common.bean.ResponseJson;
import cn.threewaters.qtyy.dic.one.queryparam.QuerySjxq;
import cn.threewaters.qtyy.dic.one.queryresult.ResultSjxqSanwen;
import cn.threewaters.qtyy.service.business.SjxqService;
import cn.threewaters.qtyy.web.facade.Point;
import cn.threewaters.qtyy.web.facade.TempAnaly;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "数据详情")
@Controller
@RequestMapping(value = "/sjxq")
public class SjxqController {

	@Autowired
	private SjxqService sjxqService;

	@ApiIgnore
	@ApiOperation(value = "数据详情页面")
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public ModelAndView main() {
		return new ModelAndView("business/sjxq/sjxq");
	}

	@ApiOperation(value = "切面")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "QuerySjxq", value = "数据详情查询条件", required = true, dataType = "QuerySjxq") })
	@RequestMapping(value = "/qiemian", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getQiemian(QuerySjxq querySjxq) throws Exception {
		// List<ResultSjxqQiemian> queryQiemian =
		// sjxqService.queryQiemian(querySjxq);

		TempAnaly tempAnaly = new TempAnaly();
		int cablehang = 4;
		int cablelie = 5;
		int cableceng = 3;
		StringBuilder tempdata = new StringBuilder();
		for (int i = 1; i <= cablehang * cablelie; i++) {
			for (int j = 1; j <= cableceng; j++) {
				tempdata.append("$");
				if (j == 1) {
					tempdata.append(i + ":" + j + ":" + i);
				} else {
					tempdata.append(i + ":" + j + ":" + i + "." + j);
				}

			}
		}
		System.out.println(tempdata.toString());

		String stapoint = "3"; // 起始点：0左上，1左下，2右上，3右下
		String sortdir = "0"; // 排序方向：0横，1纵
		String sortrule = "0"; // 排序规则：0S,1E
		String pointrule = "0"; // 层号规则：0从上到下，1从下到上

		Point[][][] analysis = tempAnaly.analysis(tempdata.toString(), cablehang, cablelie, cableceng, stapoint,
				sortdir, pointrule, sortrule);

		List<Point> list = Lists.newArrayList();
		for (int i = 0; i < analysis.length; i++) {
			for (int j = 0; j < analysis[i].length; j++) {
				for (int k = 0; k < analysis[i][j].length; k++) {
					list.add(analysis[i][j][k]);
				}
			}
		}

		Map<String, Object> resultMap = Maps.newHashMap();
		resultMap.put("points", list);
		resultMap.put("cablehang", cablehang);
		resultMap.put("cablelie", cablelie);
		resultMap.put("cableceng", cableceng);

		return ResponseJson.builder().success(true).obj(resultMap).build();
	}

	@ApiOperation(value = "三温")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "QuerySjxq", value = "数据详情查询条件", required = true, dataType = "QuerySjxq") })
	@RequestMapping(value = "/sanwen", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getSanwen(QuerySjxq querySjxq) throws Exception {
		List<ResultSjxqSanwen> querySanwen = sjxqService.querySanwen(querySjxq);
		return ResponseJson.builder().success(true).obj(querySanwen).build();
	}

}
